package com.example.mymvp2;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.mymvp2.R;
import com.example.mymvp2.bean.SendBean;
import com.example.mymvp2.modle.ResultCallback;
import com.example.mymvp2.modle.ResultToken;
import com.example.mymvp2.presenter.PresenterCallbackView;
@Route(path = "/mvp2/test/MainActivity")
public class MainActivity extends AppCompatActivity implements View.OnClickListener, BaseViewListener<SendBean> {

    public static String TAG = "tgw" + MainActivity.class.getName();

    private TextView tvResult;
    private Button btSucceed, btFail, btException;
    private EditText editContent;


    private PresenterCallbackView presenterCallbackView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult = findViewById(R.id.tv_result);
        btSucceed = findViewById(R.id.bt_succeed);
        btFail = findViewById(R.id.bt_fail);
        btException = findViewById(R.id.bt_exception);
        editContent = findViewById(R.id.edit_content);
        btSucceed.setOnClickListener(this);
        btFail.setOnClickListener(this);
        btException.setOnClickListener(this);


        presenterCallbackView = new PresenterCallbackView();
//        ResultToken.getToken(MainActivity.this);


    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.bt_succeed) {

            requestData(this, 1);//回调方法，相当于加了识别 token的那种
            presenterCallbackView.requestParams(1);
        } else if (viewId == R.id.bt_fail) {
            requestData(this, 2);
            presenterCallbackView.requestParams(2);
        }
        if (viewId == R.id.bt_exception) {
            requestData(this, 3);
            presenterCallbackView.requestParams(3);
        }

        //该方法 单纯的回调数据，不需要验证
//        presenterCallbackView.execute(this);
    }


    /**
     * 可以直接用这个方法  取消注册继承接口等步骤
     */
    public void requestData(Activity activity, int requestType) {

        ResultCallback
                // 设置请求标识token
                .request(ResultToken.getToken(new PresenterCallbackView()))
                // 添加请求参数，没有则不添加
                .requestParams(requestType)
                // 注册监听回调
                .execute(new BaseViewListener<SendBean>() {
                    @Override
                    public void showSucceedData(SendBean sendBean) {
                        tvResult.setText(sendBean.toString());
                    }

                    @Override
                    public void showFailedData(SendBean sendBean) {
                        tvResult.setText(sendBean.toString());
                    }

                    @Override
                    public void showExceptionData(SendBean sendBean) {
                        tvResult.setText(sendBean.toString());
                    }

                    @Override
                    public void showCompleteData(SendBean sendBean) {
                        Toast.makeText(MainActivity.this,"showComplete: " + sendBean.toString(),Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "showComplete: " + sendBean.toString());
                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void showSucceedData(SendBean sendBean) {
        tvResult.setText(sendBean.toString());
    }

    @Override
    public void showFailedData(SendBean sendBean) {
        tvResult.setText(sendBean.toString());
    }

    @Override
    public void showExceptionData(SendBean sendBean) {
        tvResult.setText(sendBean.toString());
    }

    @Override
    public void showCompleteData(SendBean sendBean) {
        Log.d(TAG, "showComplete: " + sendBean.toString());
    }
}
